package Chapter11;

/**
 * @author 杨森
 * @date 2022年10月20日 17:39
 */
public class Homework06 {
    public static void main(String[] args) {

        Person person = new Person("唐僧", new Horse());
        person.common();
        person.passRiver();
        person.fly();
    }
}

interface Vehicles {
    public void work();
}

class Horse implements Vehicles {
    @Override
    public void work() {
        System.out.println("一般情况下使用马");
    }
}

class Boat implements Vehicles {
    @Override
    public void work() {
        System.out.println("用船过河");
    }
}

class Plane implements Vehicles{
    @Override
    public void work() {
        System.out.println("飞机过火焰山");
    }
}

class VehiclesFactory {
    private static Horse horse = new Horse();
    private VehiclesFactory() {
    }

    public static Horse getHorse() {
        return horse;
    }

    public static Boat getBoat() {
        return new Boat();
    }

    public static Plane getPlane() {
        return new Plane();
    }
}

class Person {
    private String name;
    private Vehicles vehicles;

    public Person(String name, Vehicles vehicles) {
        this.name = name;
        this.vehicles = vehicles;
    }

    public void passRiver() {
//        判断当前的vehicles属性是否为空，如果为空就获取一艘船
//        if (vehicles == null) {
//            vehicles = VehiclesFactory.getHorse();
//        } else if (vehicles instanceof Horse) {
//            vehicles = VehiclesFactory.getBoat();
//        }
        if (!(vehicles instanceof Boat)) {
            vehicles = VehiclesFactory.getBoat();
        }
        vehicles.work();
    }

    public void common() {
//        判断当前的vehicles属性是否为空，如果为空就获取一匹马
//        if (vehicles == null) {
        if (!(vehicles instanceof Horse)) {
//            多态
            vehicles = VehiclesFactory.getHorse();
        }
        vehicles.work();
    }

    public void fly(){
        if (!(vehicles instanceof Plane)){
            vehicles=VehiclesFactory.getPlane();
        }
        vehicles.work();
    }
}
